package com.genghis.ptas.study.service.impl;

import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.course.entity.CourseInfo;
import com.genghis.ptas.course.service.CourseService;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.service.NoteService;
import com.genghis.ptas.study.dao.StudyRecordDao;
import com.genghis.ptas.study.entity.StudyRecord;
import com.genghis.ptas.study.service.StudyRecordService;
import com.genghis.ptas.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("studyRecordService")
public class StudyRecordServiceImpl implements StudyRecordService {

    @Autowired
    private StudyRecordDao studyRecordDao;

    @Autowired
    private CourseService courseService;

    @Autowired
    private NoteService noteService;

    @Autowired
    private ClassifyService classifyService;

    @Override
    public void addStudyRecord(StudyRecord studyRecord) {
        StringBuilder study = new StringBuilder("");
        String date = DateUtil.getSystemDate(DictConstant.DATE_FORMAT);
        studyRecord.setStudyDate(date);
        study.append("在").append(date).append("时刻，").append(studyRecord.getStudyRecord());
        studyRecord.setStudyRecord(study.toString());
        studyRecordDao.addStudyRecord(studyRecord);
    }

    @Override
    public List<StudyRecord> findStudyRecordList(StudyRecord studyRecord) {
        List<StudyRecord> studyRecords = studyRecordDao.findStudyRecordList(studyRecord);
        for (StudyRecord studyRecordTemp : studyRecords) {
            if (3 == studyRecordTemp.getCategory()) {
                CourseInfo courseInfo = courseService.findCourseByCourseId(studyRecordTemp.getCourseId());
                studyRecordTemp.setCourseInfo(courseInfo);
                studyRecordTemp.setShowColor(getColorMap().get(courseInfo.getCourseType()));
            }
            if (1 == studyRecordTemp.getCategory() || 2 == studyRecordTemp.getCategory()) {
                Note note = noteService.findNoteById(studyRecordTemp.getCourseId());
                studyRecordTemp.setNote(note);
                studyRecordTemp.setShowColor(getColorMap().get(note.getNoteType()));
            }
            String date = studyRecordTemp.getStudyDate();
            studyRecordTemp.setStudyDate(date.substring(10, 16));
            studyRecordTemp.setDate(date.substring(0, 10));
        }
        return studyRecords;
    }

    private Map<Integer, String> getColorMap() {
        Map<Integer, String> map = new HashMap();
        List<String> colorList = new ArrayList<String>();
        colorList.add("yellow");
        colorList.add("blue");
        colorList.add("green");
        colorList.add("purple");
        colorList.add("red");
        colorList.add("grey");
        colorList.add("blue");
        colorList.add("black");
        colorList.add("white");
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        int i = 0, len = colorList.size();
        for (TypeValues typeValues : typeValuesList) {
            map.put(typeValues.getId(), colorList.get(i % len));
            i++;
        }
        return map;
    }
}
